package com.hotchpotch.activity;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.support.v4.view.ViewCompat;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;

import com.hotchpotch.R;
import com.hotchpotch.base.BaseActivity;
import com.hotchpotch.util.Logg;

/**
 * Created by Administrator on 2017/10/11 0011.
 */

public class RotateImageActivity extends BaseActivity {
    private static final String TAG = "旋转图片";
    private ImageView imgPan, imgStart;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_rotate_image;
    }

    @Override
    protected void initView() {
        imgPan = (ImageView) findViewById(R.id.zhuanpan);
        imgStart = (ImageView) findViewById(R.id.start);
        imgStart.setSelected(true);
    }

    @Override
    protected void initEvents() {
        imgStart.setOnClickListener(clickListener);
    }

    @Override
    protected void initData() {

    }

    private View.OnClickListener clickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.start:
                    if (imgStart.isSelected()) {
                        imgStart.setSelected(false);
                        showSnackbar(v, "开始");
                        startAnim();
                    } else {
                        imgStart.setSelected(true);
                        showSnackbar(v, "暂停");
                        endAnim();
                    }
                    break;
            }
        }
    };

//    private ObjectAnimator animtor;
    private void startAnim() {
        Logg.e(TAG, "度数 = " + (-360f * 5 - 60));
        ObjectAnimator animtor = ObjectAnimator.ofFloat(imgPan, "rotation", 0f, -360f * 5 - 60);
        animtor.setInterpolator(new AccelerateDecelerateInterpolator());
        animtor.setRepeatMode(ObjectAnimator.RESTART);
        animtor.setDuration(5000);
        animtor.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
                Logg.e(TAG, "动画开始");
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                Logg.e(TAG, "动画结束");
            }

            @Override
            public void onAnimationCancel(Animator animation) {
                Logg.e(TAG, "动画取消");
            }

            @Override
            public void onAnimationRepeat(Animator animation) {
                Logg.e(TAG, "动画重复");
            }
        });
        animtor.start();
    }

    private void endAnim() {
        /*if (animtor != null) {
            animtor.end();
        }*/
    }
}
